package com.example.demo.controller;

import com.alibaba.fastjson2.JSONObject;
import com.example.demo.entity.Message;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/setting")
public class WarningSettingController {
    @Autowired
    private  StringRedisTemplate redisTemplate;

    @RequestMapping("/level")
    public void level(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String lowlevel = request.getParameter("lowlevel");
        String middellevel = request.getParameter("middellevel");
        if(lowlevel!=null&&!"".equals(lowlevel)){
            redisTemplate.opsForValue().set("lowlevel",lowlevel);
        }
        if(middellevel!=null&&!"".equals(middellevel)){
            redisTemplate.opsForValue().set("middellevel",middellevel);
        }
        response.getWriter().write(JSONObject.toJSONString(Message.success("success setting")));
    }

    @RequestMapping("/condition")
    public void condition(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String gredit = request.getParameter("gredit");
        if(gredit!=null&&!"".equals(gredit)){
            redisTemplate.opsForValue().set("gredit",gredit);
        }
        response.getWriter().write(JSONObject.toJSONString(Message.success("success setting")));
    }

    @RequestMapping("/sendMethod")
    public void sendMethod(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String method = request.getParameter("method");
        if(method!=null&&!"".equals(method)){
            redisTemplate.opsForValue().set("method",method);
        }
        response.getWriter().write(JSONObject.toJSONString(Message.success("success setting")));
    }

    @RequestMapping("/getSetting")
    public void getSetting(HttpServletRequest request, HttpServletResponse response)  throws IOException {
        String lowlevel = redisTemplate.opsForValue().get("lowlevel");
        String middellevel = redisTemplate.opsForValue().get("middellevel");
        String methodway = redisTemplate.opsForValue().get("method");
        String gretdit = redisTemplate.opsForValue().get("gredit");
        List<String> setResults = new ArrayList<>(4);
        if (lowlevel!=null&&!"".equals(lowlevel)) {
            setResults.add(lowlevel);
        } else {
            setResults.add("1");
        }
        if (middellevel!=null&&!"".equals(middellevel)) {
            setResults.add(middellevel);
        } else {
            setResults.add("3");
        }
        if (methodway!=null&&!"".equals(methodway)) {
            setResults.add(methodway);
        } else {
            setResults.add("系统消息");
        }
        if (gretdit!=null&&!"".equals(gretdit)) {
            setResults.add(gretdit);
        } else {
            setResults.add("2");
        }
        response.getWriter().write(JSONObject.toJSONString(Message.success(setResults)));
    }

}
